<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: AddGoods.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\home\model\page\goods;

use app\api\model\dao\Goods;
use app\api\model\dao\GoodsSpec;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class AddGoods extends PageBase
{

    private $objGoods;
    private $objGoodsSpec;

    public function __construct()
    {
        parent::__construct();
        $this->objGoods = new Goods();
        $this->objGoodsSpec = new GoodsSpec();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cat_id' => 'require|number',
            'goods_name' => 'require',
            'brand_id' => 'require',
            'goods_number' => 'require|number',
            'market_price' => 'require|float',
            'shop_price' => 'require|float',
            'keywords' => 'require',
            'goods_brief' => 'require',
            'goods_desc' => 'require',
            'goods_thumb' => 'require',
            'goods_img' => 'require',
            'is_on_sale' => 'require|number',
            'is_hot' => 'require|number',
            'is_new' => 'require|number',
            'is_promote' => 'require|number',
            'is_certified' => 'require|number',
            'is_free_shipping' => 'require|number',
            'is_special_packing' => 'require|number',
            'sort_order' => 'require|number',
            'specs' => 'require',
        ];
        $msg = [
            'cat_id.require' => '商品分类ID必填',
            'goods_name.require' => '商品名称必填',
            'brand_id.require' => '商品品牌ID必填',
            'goods_number.require' => '商品数量必填',
            'market_price.require' => '商品市场价格必填',
            'shop_price.require' => '商品商城价格必填',
            'keywords.require' => '商品关键字描述必填',
            'goods_brief.require' => '商品简介必填',
            'goods_desc.require' => '商品详情必填',
            'goods_thumb.require' => '商品缩略图必传必填',
            'goods_img.require' => '商品图片必填',
            'is_on_sale.require' => '商品是否在售必填',
            'is_hot.require' => '商品是否热销必填',
            'is_new.require' => '商品是否新品必填',
            'is_promote.require' => '商品是否促销必填',
            'is_certified.require' => '商品是否认证正品必填',
            'is_free_shipping.require' => '商品是否免配送费必填',
            'is_special_packing.require' => '商品是否特殊包装必填',
            'sort_order.require' => '商品排序必填',
            'specs.require' => '商品规格必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            //return $this->getRes(Error::ERR_NO_AUTH);
        }

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }
        $specs = json_decode($param['specs'], true);
        if (empty($specs)) {
            return $this->getRes(Error::ERR_PARAM, "规格" . Error::$arr_err_msg[Error::ERR_PARAM]);
        }
        $param['goods_sn'] = date('YmdHis') . rand(1000, 9999);
        $goods_id = $this->objGoods->addGoods($param);
        foreach ($specs as $key => $spec) {
            $specs[$key]['goods_id'] = $goods_id;
        }
        $ret = $this->objGoodsSpec->addGoodsSpecBatch($specs);
        if ($ret === false) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();
    }
}
